package community;

import java.net.*;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

import util.*;
//커뮤니티 가입과 정보변경, 탈퇴 에 관련된 클래스
public class CommunityJoin extends DispatchAction{
	//커뮤니티 가입 양식을 가져오는 메소드
	public ActionForward getForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommunityGo.setMemberCommunity(request);
		MemberCommunityForm mcForm = (MemberCommunityForm)form;
		MemberCommunityDAO mcDAO = new MemberCommunityDAO();
		CommunityDAO coDAO = new CommunityDAO();
		
		int cono = Integer.parseInt(request.getParameter("cono"));
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		//어떤 커뮤니티인지를 알려주는 cono 셋팅
		mcForm.setCono(cono);
		
		MemberCommunity mc = new MemberCommunity();
		mc.setCono(cono);
		mc.setMid(user.getMid());
		
		mc = mcDAO.getMemberCommunity(mc) ;
		
		//MemberCommunity 정보 셋팅
		mcForm.setMid(mc.getMid());
		mcForm.setMcnick(mc.getMcnick());
		mcForm.setLvno(mc.getLvno());
		mcForm.setConame(coDAO.getCommuniytName(cono));
		
		if(mcForm.getNickcheck() ==null || mcForm.getNickcheck().equals("")){
			//닉네임 체크 여부 셋팅
			mcForm.setNickcheck("first");
		}
		
		
		return mapping.findForward("form");
	}
	
	//커뮤니티 탈퇴할 때 보여주는 폼
	public ActionForward getDelForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		MemberCommunityForm mcForm = (MemberCommunityForm)form;
		
		int cono = Integer.parseInt(request.getParameter("cono"));
		mcForm.setCono(cono);
		
		return mapping.findForward("delform");
	}
	
	public ActionForward receiveData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		MemberCommunityForm mcForm = (MemberCommunityForm)form;
		MemberCommunityDAO mcDAO = new MemberCommunityDAO();
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		MemberCommunity mc = new MemberCommunity();
		//입력한 정보를 셋팅
		mc.setMid(user.getMid());
		mc.setMcnick(mcForm.getMcnick());
		mc.setCono(mcForm.getCono());
		mc.setLvno(mcForm.getLvno());
//		
//		System.out.println(mc.getMid());
//		System.out.println(mc.getMcnick());
//		System.out.println(mc.getCono());
//		System.out.println(mc.getLvno());
//		
		//신규가입자 이면 Lv를 1로 지정하여 입력
		//신규가입자가 아니면 수정
		if(mc.getLvno()==0){
			mc.setLvno(1);
			mcDAO.insert(mc);
		}else{
			mcDAO.updateNick(mc);
		}
		
		return mapping.findForward("ok");
	
	}
	
	//커뮤니티 탈퇴 했을때 발생하는 메소드
	public ActionForward delData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		MemberCommunityDAO mcDAO = new MemberCommunityDAO();
		MemberCommunity mc = new MemberCommunity();
		int cono = Integer.parseInt(request.getParameter("cono"));
		User user = (User)request.getSession().getAttribute("user");
		
		mc.setMid(user.getMid());
		mc.setCono(cono);
				
		mcDAO.delete(mc);
		
		ActionForward actionforward = new ActionForward();
		actionforward.setPath("/communitymain.do");
		return actionforward;
	
	}
	
	//닉네임 중복검사하는 메소드
	public ActionForward nickCheck(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		MemberCommunityForm mcForm = (MemberCommunityForm)form;
		MemberCommunityDAO mcDAO = new MemberCommunityDAO();
		CommunityDAO coDAO = new CommunityDAO();
		
		int cono = Integer.parseInt(request.getParameter("cono"));
		String mcnick = URLDecoder.decode(request.getParameter("mcnick"),"utf-8");
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		mcForm.setCono(cono);
		
		MemberCommunity mc = new MemberCommunity();
		mc.setCono(cono);
		mc.setMid(user.getMid());
		
		mc = mcDAO.getMemberCommunity(mc) ;
		
		mcForm.setMid(mc.getMid());
		mcForm.setLvno(mc.getLvno());
		mcForm.setConame(coDAO.getCommuniytName(cono));
		
		int count = mcDAO.nickCheck(cono, mcnick);
		if(count>0){
			//중복 되었음
			mcForm.setNickcheck("false");
			mcForm.setMcnick(mc.getMcnick());
		}else{
			//중복된 닉네임 없음
			mcForm.setNickcheck("true");
			mcForm.setMcnick(mcnick);
		}			
		
		return mapping.findForward("form");
	}
	
	

}
